﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PassportMap = System.Collections.Generic.Dictionary<int, int>;

/**
*@file       passport.cs
*@brief      passport verify stuff
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/02/13
*/

public class PassportVerify
{
    #region private variable
    //passport map
    private PassportMap m_map = new PassportMap();
    //lock object
    private Object m_lockObj = new Object();
    #endregion

    #region external call
    /// <summary>
    /// generate a passport
    /// </summary>
    /// <param name="usr_id">identifier of the user</param>
    /// <returns></returns>
    public int GeneratePassport(int usr_id)
    {
        lock (m_lockObj)
        {
            Random rad = new Random();
            int passport = rad.Next();
            m_map[usr_id] = passport;
            return passport;
        }
    }

    /// <summary>
    /// remove a passport
    /// </summary>
    /// <param name="usr_id">identifier of the user</param>
    public void RemovePassport(int usr_id)
    {
        lock (m_lockObj)
        {
            if (m_map.ContainsKey(usr_id) == true)
                m_map.Remove(usr_id);
        }
    }

    /// <summary>
    /// check the passport is valid
    /// </summary>
    /// <param name="Name">name of the user</param>
    /// <param name="pass_port">passport to verify</param>
    /// <returns></returns>
    public bool CheckPassport(int usr_id, int pass_port)
    {
        lock (m_lockObj)
        {
            if (m_map.ContainsKey(usr_id) == true)
                return m_map[usr_id] == pass_port;
        }
        return false;
    }
    #endregion
}